package com.zpy.SmartChargeStation.netty.server.handlers;

import com.zpy.SmartChargeStation.netty.server.utils.HTTPUtils;
import com.zpy.SmartChargeStation.protocol.mqtt.message.ChargePayload;
import com.zpy.SmartChargeStation.protocol.protobuf.ChargingCmdProtobuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * description: 专门处理Protobuf反序列之后的数据
 */

@Slf4j
public class ProtobufHandler extends SimpleChannelInboundHandler<ChargingCmdProtobuf.ChargingCmd> {
    @Override
    protected void channelRead0(
            ChannelHandlerContext channelHandlerContext,
            ChargingCmdProtobuf.ChargingCmd chargingCmd) throws Exception {

        log.info(">>>>>微信小程序发送的充电指令："+chargingCmd.getCmd());

        //采用OKHTTP类库去调用mqtt客户端模块的发送消息API

        String cmd = chargingCmd.getCmd();
        //取第一个字符
        char chatStr = cmd.charAt(0);
        log.info(">>>>>充电指令的第1个字符："+chatStr);
        //转换为byte
        byte cmdByte = (byte)chatStr;
        log.info(">>>>>充电指令转换为byte："+cmdByte);

        //私有协议的实体类
        ChargePayload chargePayload =  new ChargePayload();
        chargePayload.setCmd(cmdByte);

        //请求mqtt发送api
        HTTPUtils.doPOST(chargePayload);

    }
}
